package com.yfuxdeer.youchat.ui.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import android.app.Fragment;

import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.yfuxdeer.youchat.R;
import com.yfuxdeer.youchat.adapter.MomentListAdapter;
import com.yfuxdeer.youchat.pojo.MomentItem;
import com.yfuxdeer.youchat.service.MomentService;
import com.yfuxdeer.youchat.utils.Store;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MomentFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MomentFragment extends Fragment {

    private static final String TAG = MomentFragment.class.getSimpleName();
    public static Handler handler;
    private List<MomentItem> momentItemList;
    Store store = Store.getInstance();
    private MomentListAdapter adapter;
    private ListView listView;

    static {
        handler = new Handler();
    }


    public MomentFragment() {
    }

    public static MomentFragment newInstance() {
        MomentFragment fragment = new MomentFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_moment, container, false);

        Log.e(TAG, "onCreateView: 动态页面");

        //注册广播
        inflater.getContext().registerReceiver(findAllMomentReceiver, new IntentFilter("com.yfuxdeer.youchat.findAllMomentReceiver"));
        listView = view.findViewById(R.id.list);
        return view;
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.e(TAG, "onStop: 动态页面");
    }

    private void init() {
        //调用服务请求网络查询 加载动态信息
        Intent intent = new Intent(getActivity().getApplicationContext(), MomentService.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("method", MomentService.METHOD.FINDALL);
        bundle.putString("hostId", store.getCurrentUser().getUsername());
        intent.putExtras(bundle);
        getActivity().getApplicationContext().startService(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: ");
        init();
    }

    private BroadcastReceiver findAllMomentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (getActivity() == null || getActivity().isFinishing()) {
                return;
            }
            Log.e(TAG, "onReceive: " + "接收到广播");
            Bundle bundle = intent.getExtras();
            MomentService.RESULT result = (MomentService.RESULT) bundle.getSerializable("result");
            switch (result) {
                case SUCCESS:
                    List<MomentItem> momentItemList = (List<MomentItem>) bundle.getSerializable("momentItemList");
                    MomentFragment.this.momentItemList = momentItemList;
                    adapter = new MomentListAdapter(MomentFragment.this.momentItemList, getActivity().getApplicationContext());
                    listView.setAdapter(adapter);
                    break;
                case NETWORK_EXCEPTION:
                    Toast.makeText(getActivity().getApplicationContext(), "网络异常", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

}